package com.peng.service.impl;

import com.peng.exception.GlobalException;
import com.peng.pojo.User;
import com.peng.mapper.UserMapper;
import com.peng.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.utils.CookieUtil;
import com.peng.utils.MD5Utils;
import com.peng.utils.UUIDUtil;
import com.peng.vo.LoginVo;
import com.peng.vo.result.RespBean;
import com.peng.vo.result.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Dengp
 * @since 2022-07-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录验证
     * @param loginVo
     * @param request
     * @param response
     * @return
     */
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        User user = this.getById(mobile);
        if (user==null){
            throw new GlobalException(RespBeanEnum.LOGINVO_ERROR);
        }
        if (!MD5Utils.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGINVO_ERROR);
        }
        String uuid = UUIDUtil.uuid();
        //request.getSession().setAttribute(uuid,user);
        redisTemplate.opsForValue().set("user:"+uuid,user);
        CookieUtil.setCookie(request,response,"userTicket",uuid);
        return RespBean.success(uuid);
    }

    /**
     * 根据ticket过去用户信息
     * @param request
     * @param response
     * @param ticket
     * @return
     */
    @Override
    public User getUserTicket(HttpServletRequest request, HttpServletResponse response, String ticket) {
        if (StringUtils.isEmpty(ticket)){
            return  null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + ticket);
        if(user!=null){
            CookieUtil.setCookie(request,response,"userTicket",ticket);
        }
        return user;
    }

    /**
     * 功能描述:
     *
     * @param: 更新密
     * @since: 1.0.0
     * @Author:zhoubin
     */
    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request,
                                   HttpServletResponse response) {
        User user = getUserTicket( request, response,userTicket);
        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Utils.inputPassToDbPass(password, user.getSalt()));
        boolean result = this.updateById(user);
        if (result) {
            //删除Redis
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
